<?php
namespace app\im\model;

use app\BaseModel;
use think\facade\Db;
class ImMessage extends BaseModel
{
	//定义表名称
	protected $name = 'longbing_card_message';
	
	//创建聊天消息
	public function createMessage($data) 
	{
		$data['create_time'] = time();
		return $this->createRow($data);
	}
	
	//修改聊天消息
	public function updateMessage($filter ,$data) 
	{
		$filter['deleted'] = 0;
		return $this->updateRow($filter ,$data);
	}
	
	//标记已读消息
	public function readMessage($filter) 
	{
		$filter['status'] = 1;
		return $this->updateMessage($filter ,['status' => 2]);
	}
	
	//撤销消息
	public function recallMessage($filter)
	{
		$filter['deleted'] = 0;
		return $this->updateMessage($filter, ['status' => 3]);
	}
	
	//查询聊天消息列表
	public function listMessage($filter ,$page_config)
	{
		$start_row = 0;
		$page_count = 10;
		if(isset($page_config['page_count']) && !empty($page_config['page_count']) && $page_config['page_count'] > 0) $page_count = $page_config['page_count'];
		if(isset($page_config['page']) && !empty($page_config['page']) && $page_config['page'] > 0) $start_row = ($page_config['page'] -1) * $page_count;
//		$filter['deleted'] = 0;
		$result = $this->where($filter)
            ->where(['deleted'=>0])
			->order('id desc')
			->limit($start_row ,$page_count)
			->select();
		if(!empty($result))
		{
			$result = $result->toArray();
			$result = array_reverse($result);
		}
		return $result;
	}

    //查询聊天消息列表
    public function listMessageV2($filter ,$page_config,$dis)
    {
        $start_row = 0;
        $page_count = 10;
        if(isset($page_config['page_count']) && !empty($page_config['page_count']) && $page_config['page_count'] > 0) $page_count = $page_config['page_count'];
        if(isset($page_config['page']) && !empty($page_config['page']) && $page_config['page'] > 0) $start_row = ($page_config['page'] -1) * $page_count;
//		$filter['deleted'] = 0;
        $result = $this->where($filter)
            ->whereOr(function ($query) use ($dis){
                $query->where($dis);
            })
            ->order('id desc')
            ->limit($start_row ,$page_count)
            ->select();
        if(!empty($result))
        {
            $result = $result->toArray();
            $result = array_reverse($result);
        }
        return $result;
    }


    //获取聊天消息总量
	public function listMessageCount($filter) 
	{
		$filter['deleted'] = 0;
		$count = $this->where($filter)->count();
		return $count;
	}
	
	//删除聊天消息
	public function delMessage($filter) 
	{
		$filter['deleted'] = 0;
		return $this->deleteRow($filter);
	}
	
	//查询最后一条消息
	public function lastMessage($filter)
	{
		$filter['deleted'] = 0;
		$result = $this->where($filter)->order('create_time', 'desc')->field(['create_time' ,'content' ,'message_type'])->find();
		return $result;
	}
	//获取未读消息列表
	public function listNotReadMessage($filter)
	{
		$filter['deleted'] = 0;
		$filter['status'] = 1;
		$result = $this->where($filter)
			->order('create_time' ,'asc')
			->select();
		if(!empty($result)) $result = $result->toArray();
		$this->updateMessage($filter, ['status' =>2]);
		return $result;
	}
	
	//真删除
	public function destroyMessage($filter)
	{
		return $this->destoryRow($filter);
	}
}
